#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <string>
#include <cstring>
#include <thread>
#include <stdlib.h>
#include <iostream>

#define VMRSS_LINE 17
#define VMSIZE_LINE 13
#define PROCESS_ITEM 14

#include "metrics/utility/Strings.hpp"

int main(int argc, char *argv[])
{
    std::string cmd("ps -aux | grep ");
    auto process_id = getpid();
    cmd.append(std::to_string(process_id));

    char buf[1024] = {0};

    FILE *fp = NULL;
    
    if ((fp = popen(cmd.c_str(), "r")) == NULL)
    {
        printf("popen error!\n");
        return 0;
    }

    fgets(buf, sizeof(buf), fp);

    
    std::string res(buf);
    auto strs = metrics::Strings::split(buf, " ");

    int i = 0;
    for(auto str : strs)
    {
        std::cout << i++ << " ->" << str << std::endl;
    }

    std::cout << "CPU% = " << strs[8] << std::endl;
    std::cout << "MEM% = " << strs[10] << std::endl;
    std::cout << "RSS = " << strs[12] << std::endl;
    std::cout << "VSZ = " << strs[14] << std::endl;
    pclose(fp);

    return 0;
}